package com.waya.demo.reactor;

import java.io.IOException;
import java.net.ServerSocket;

public class Server implements Runnable{
	
	private final int port;
	
	private ServerSocket serverSocket;
	
	public Server(int port){
		this.port = port;
		try {
			this.serverSocket = new ServerSocket(port);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void run() {
		try {
            while (!Thread.interrupted()) {//测试当前线程是否处于中断状态，执行后将中断状态标志为false。如果连续两次调用interrupted（）方法，第一次调用时，如果当前线程接受到了中断信号，那么该方法会返回true，同时清除当前线程被标记的中断状态。第二次调用时，就会返回false了。
                new Thread(new Handler(serverSocket.accept())).start();
            } 
        } catch (IOException e) {
            e.printStackTrace();
        }

	}
	
	public int getPort() {
        return port;
    }
 
    public static void main(String[] args) {
        new Thread(new Server(9001)).start();
    }


}
